package com.blgroup.oa.web.controller;


import com.blgroup.oa.domain.vo.UserVO;
import com.blgroup.oa.service.GroupService;
import com.blgroup.oa.service.UserService;
import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * 用户Controller
 *
 * @author tangyulong
 * @version v1.0
 * @ClassName: UserController
 * @date 2018年5月16日 下午11:35:41
 */
@RestController
@RequestMapping(value = "/user")
public class UserController extends BaseController {

    @Autowired
    UserService userService;

    @Autowired
    GroupService groupService;

    /**
     * 方法描述：   跳转用户列表页面
     */
    @GetMapping(value = "/list")
    public ModelAndView list(@Valid @ModelAttribute("dto") UserVO dto, BindingResult bindingResult, Model model, HttpServletRequest request) {
        //查询用户列表
        ModelAndView mv = new ModelAndView("user/user");
        try {
            List<UserVO> list = userService.list();
            model.addAttribute("users", list);
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return mv;
    }

    /**
     * 方法描述：   跳转用户新增页面
     */
    @GetMapping(value = "/toAddUser")
    public ModelAndView toAddUser(@Valid @ModelAttribute("dto") UserVO dto, BindingResult bindingResult, Model model, HttpServletRequest request) {
        //查询用户列表
        ModelAndView mv = new ModelAndView("user/addUser");
        try {
            List<Group> list = groupService.list();
            model.addAttribute("groups", list);
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return mv;
    }

    /**
     * 方法描述：   用户新增
     */
    @PostMapping(value = "/addUser")
    public ModelAndView addUser(@Valid @ModelAttribute("dto") UserVO dto, BindingResult bindingResult, Model model, HttpServletRequest request) {
        //查询用户列表
        ModelAndView mv = new ModelAndView("user/addUser");
        try {
            userService.save(dto);
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return list(dto, bindingResult, model, request);
    }


    /**
     * 方法描述：   用户删除
     */
    @GetMapping(value = "/deleteUser")
    public ModelAndView deleteUser(@Valid @ModelAttribute("dto") UserVO dto, BindingResult bindingResult, Model model, HttpServletRequest request) {
        //查询用户列表
        ModelAndView mv = new ModelAndView("user/addUser");
        try {
            userService.delete(dto.getUserId());
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return list(dto, bindingResult, model, request);
    }

    /**
     * 方法描述：   用户登录
     */
    @RequestMapping(value = "/login")
    public ModelAndView login(@Valid @ModelAttribute("dto") UserVO dto, BindingResult bindingResult, Model model, HttpServletRequest request) {
        //查询用户列表
        ModelAndView mv = new ModelAndView("login");
        try {
            User user = this.userService.loginValidate(dto);
            if (user != null) {
                Group group = this.userService.getGroup(user.getId());
                request.getSession().setAttribute("user",user);
                request.getSession().setAttribute("userId",user.getId());
                request.getSession().setAttribute("group",group);
                mv.setViewName("index");
            } else {
                model.addAttribute("loginMsg", "用户名或密码错误");

            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return mv;
    }
}
